cmake_minimum_required (VERSION 2.8)
project (unittest)

set(LIB_LIST)

# gtest
add_subdirectory(../deps/googletest gtest) # generate deps library
find_path(GTEST_INCLUDE_DIRS NAMES gtest.h  HINTS "../deps/googletest/googletest/include/gtest")
find_library(GTEST_LIBRARIES NAMES gtest_main HINTS "../lib")
if (GTEST_INCLUDE_DIRS AND GTEST_LIBRARIES)
    include_directories("${GTEST_INCLUDE_DIRS}")
    list(APPEND LIB_LIST gtest)
    list(APPEND LIB_LIST gtest_main)
endif()

# glog
find_path(GLOG_INCLUDE_DIRS NAMES logging.h HINTS "/usr/local/include/glog")
find_library(GLOG_LIBRARIES NAMES glog)
if (GLOG_INCLUDE_DIRS AND GLOG_LIBRARIES)
    include_directories("${GLOG_INCLUDE_DIRS}")
    list(APPEND LIB_LIST glog)
endif()

# cppzmq
find_path(CPPZMQ_INCLUDE_DIRS NAMES zmq.hpp HINTS "../deps/cppzmq")
find_library(CPPZMQ_LIBRARIES NAMES zmq)
if (CPPZMQ_INCLUDE_DIRS AND CPPZMQ_LIBRARIES)
    include_directories("${CPPZMQ_INCLUDE_DIRS}")
    list(APPEND LIB_LIST "${CPPZMQ_LIBRARIES}")
endif()

# forestdb
find_path(FORESTDB_INCLUDE_DIRS NAMES forestdb.h HINTS "../deps/forestdb/include/libforestdb")
find_library(FORESTDB_LIBRARIES NAMES forestdb HINTS "../lib")
if (FORESTDB_INCLUDE_DIRS AND FORESTDB_LIBRARIES)
    include_directories("${FORESTDB_INCLUDE_DIRS}")
    link_directories("${FORESTDB_LIBRARIES}")
    list(APPEND LIB_LIST ${FORESTDB_LIBRARIES})
endif()

# uWebsockets
find_path(UWS_INCLUDE_DIRS NAMES uWS.h HINTS "../deps/uWebsockets/src")
find_library(UWS_LIBRARIES NAMES uWS HINTS "../lib")
if (UWS_INCLUDE_DIRS AND UWS_LIBRARIES)
    include_directories("${UWS_INCLUDE_DIRS}")
    list(APPEND LIB_LIST ${UWS_LIBRARIES})
endif()

# wren
find_path(WREN_INCLUDE_DIRS NAMES wren.h HINTS "deps/wren/src/include")
find_path(WREN_INCLUDE_DIRS_2 NAMES vm.h HINTS "deps/wren/src/cli")
find_library(WREN_LIBRARIES NAMES wren HINTS "lib")
if (WREN_INCLUDE_DIRS AND WREN_INCLUDE_DIRS_2 AND WREN_LIBRARIES)
    include_directories("${WREN_INCLUDE_DIRS}")
    include_directories("${WREN_INCLUDE_DIRS_2}")
    list(APPEND LIB_LIST ${WREN_LIBRARIES})
endif()

#message(${LIB_LIST})

# application
INCLUDE_DIRECTORIES(include)
file(GLOB_RECURSE DIR_SRCS "../src/*.cc")
file(GLOB_RECURSE DIR_TESTS "../tests/*.cc")

add_executable(unittest ${DIR_SRCS} ${DIR_TESTS})
TARGET_LINK_LIBRARIES(unittest ${LIB_LIST} -lssl -lz)

# coverage
OPTION (ENABLE_GCOV "Enable gcov (debug, Linux builds only)" ON)
IF(ENABLE_GCOV)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
    list(APPEND LIB_LIST gcov)
    set(CMAKE_BUILD_TYPE Debug)
ENDIF()